﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    progressmonitorthread.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-02-19  11:36
*/
#include "progressmonitorthread.h"
#include <QDebug>
#include <QFileInfo>

ProgressMonitorThread::ProgressMonitorThread(QObject *parent) : QThread(parent)
{
    isRun = false;
}

ProgressMonitorThread::~ProgressMonitorThread()
{
    stop();
    quit();
    wait();
}

void ProgressMonitorThread::stop()
{
    isRun = false;
}

void ProgressMonitorThread::setProgressPath(const QString &value)
{
    progressPath = value;
}

void ProgressMonitorThread::run()
{
    isRun = true;
    progressManage.enableDebugPriv();

    ProgressInfo progressInfo;
    while (isRun){
        progressInfo.clear();
        if(progressPath.isEmpty() || !QFileInfo::exists(progressPath)){
                            progressInfo.setStateMsg(QString("请确认进程路径。"));
        }else{ //进程路径存在
            string stdProgressPath = progressPath.toStdString();
            DWORD progressId;
            if(progressManage.getProcessIdByPath(stdProgressPath,progressId)){ //获取进程ID成功
                progressInfo.setEnabled(true);

                uint64_t runTime;//运行时长（s）
                float cpuUsage;//cpu占用率
                uint64_t memory;//内存
                uint64_t virtualMemory;//虚拟内存
                uint64_t ioRead;//io读
                uint64_t ioWrite;//io写

                progressManage.get_cpu_usage(progressId,cpuUsage);
                progressManage.get_memory_usage(progressId,memory, virtualMemory);
                progressManage.get_io_bytes(progressId,ioRead,ioWrite);
                progressManage.getProcessTimeById(progressId,runTime);

                progressInfo.setCpuUsage(cpuUsage);
                progressInfo.setMemory(memory);
                progressInfo.setVirtualMemory(virtualMemory);
                progressInfo.setIoRead(ioRead);
                progressInfo.setIoWrite(ioWrite);
                progressInfo.setRunTime(runTime);
            }else{
                progressInfo.setEnabled(false);
                                    progressInfo.setStateMsg(QString("进程未启动。"));
            }
        }
        emit signal_progressInfo(progressInfo);
        QThread::msleep(1000);
    }
}
